package ssm.another;

/**
 * Created by 58296 on 2016/8/24.
 */
public class MailUtil {
  /*  public boolean sendEmail(String to, String subject, String message)
            throws Exception {
//        //加载配置文件
        Properties properties = new Properties();
        //String propertiesPath = "C:/Users/bear/IdeaProjects/Xfile/WebRoot/WEB-INF/classes//mail.properties";
        String propertiesPath = getClass().getClassLoader().getResource("/").getPath()+"mail.properties";
        FileInputStream input;
        input = new FileInputStream(propertiesPath);
        properties.load(input);
        //设置邮件服务器
        Properties props = new Properties();
        props.put("mail.smtp.host", properties.getProperty("mail.host"));
        //props.put("mail.smtp.host", "smtp.sina.com");
        props.put("mail.smtp.port", "25");
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.auth", "true");
        SecurityManager security = System.getSecurityManager();
        try {
            //获取默认Session
            Session session = Session.getInstance(props,new Authenticator(){
                        public PasswordAuthentication getPasswordAuthentication()
                        {
                            Properties properties = new Properties();
                            String propertiesPath = getClass().getClassLoader().getResource("/").getPath()+"mail.properties";
                            //String propertiesPath = "C:/Users/bear/IdeaProjects/Xfile/WebRoot/WEB-INF/classes//mail.properties";
                            FileInputStream input = null;
                            try {
                                input = new FileInputStream(propertiesPath);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            try {
                                properties.load(input);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            return new PasswordAuthentication(properties.getProperty("mail.username"), properties.getProperty("mail.password")); //发件人邮件用户名、密码
                            //return new PasswordAuthentication("youthpoem@sina.com", "06241994bear"); //发件人邮件用户名、密码
                        }
                    }
            );
            //创建默认的MimeMessage对象
            MimeMessage mimeMessage = new MimeMessage(session);
            //设置头部信息
            mimeMessage.setFrom(new InternetAddress(properties.getProperty("mail.username")));
            //mimeMessage.setFrom(new InternetAddress("youthpoem@sina.com"));
            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            mimeMessage.setSubject(subject);
            mimeMessage.setText(message);

//            Transport transport = session.getTransport("smtp");
//            //transport.connect(properties.getProperty("mail.host"),properties.getProperty("mail.password") );
//            transport.connect("smtp.sina.com", "06241994bear");
            Transport transport = session.getTransport("smtp");
            //transport.connect("smtp.sina.com","06241994bear");
            transport.connect(properties.getProperty("mail.host"),properties.getProperty("mail.password") );
            Transport.send(mimeMessage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }*/
}
